package com.ynet.middleground.oss.common;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ynet.core.common.ServiceResult;

/**
 * @author liulx
 * @description 权限异常处理
 * @date 2020-10-19 11:03
 */
@Component
public class AuthorityAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setStatus(HttpStatus.OK.value());
        httpServletResponse.setHeader("Content-Type", "application/json;charset=UTF-8");
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        serviceResult.setError("ECOS1001", "无权限");
        httpServletResponse.getWriter().write(JSON.toJSONString(serviceResult));
    }

}
